package com.smarthealthcare.discharge.service.impl;

import java.util.List;

import com.smarthealthcare.cost.mapper.CostMapper;
import com.smarthealthcare.domain.Register;
import com.smarthealthcare.mapper.RegisterMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.smarthealthcare.discharge.mapper.DischargeMapper;
import com.smarthealthcare.discharge.domain.Discharge;
import com.smarthealthcare.discharge.service.IDischargeService;

/**
 * 出院管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-11-22
 */
@Service
public class DischargeServiceImpl implements IDischargeService 
{
    @Autowired
    private DischargeMapper dischargeMapper;
    @Autowired
    private RegisterMapper registerMapper;

    @Autowired
    private CostMapper costMapper;
    /**
     * 查询出院管理
     * 
     * @param id 出院管理主键
     * @return 出院管理
     */
    @Override
    public Discharge selectDischargeById(Long id)
    {
        Discharge discharge = dischargeMapper.selectDischargeById(id);
        Long patientId = discharge.getPatientId();
        Register register = registerMapper.selectRegisterById(patientId);
        discharge.setCostTotal(costMapper.getTotalCost(discharge.getPatientId()));
        discharge.setRegister(register);
        return discharge;
    }

    /**
     * 查询出院管理列表
     * 
     * @param discharge 出院管理
     * @return 出院管理
     */
    @Override
    public List<Discharge> selectDischargeList(Discharge discharge)
    {
        List<Discharge> discharges = dischargeMapper.selectDischargeList(discharge);
        for (Discharge discharge1 : discharges) {
            //  注入患者对象并且计算总金额
            Double costToal = 0D;
            discharge1.setRegister(registerMapper.selectRegisterById(discharge1.getPatientId()));
            if(costMapper.getTotalCost(discharge1.getPatientId())!=null){
                costToal = costMapper.getTotalCost(discharge1.getPatientId());
            }

            discharge1.setCostTotal(costToal);
            //  将数据库中的总金额改变
            Discharge dc = new Discharge();
            dc.setId(discharge1.getId());
            dc.setCostTotal(costToal);
            System.out.println(dc.toString());
            dischargeMapper.updateDischarge(dc);
        }
        return discharges;
    }

    /**
     * 新增出院管理
     * 
     * @param discharge 出院管理
     * @return 结果
     */
    @Override
    public int insertDischarge(Discharge discharge)
    {

        int i = dischargeMapper.insertDischarge(discharge);
        return i;
    }

    /**
     * 修改出院管理
     * 
     * @param discharge 出院管理
     * @return 结果
     */
    @Override
    public int updateDischarge(Discharge discharge)
    {
        return dischargeMapper.updateDischarge(discharge);
    }

    /**
     * 批量删除出院管理
     * 
     * @param ids 需要删除的出院管理主键
     * @return 结果
     */
    @Override
    public int deleteDischargeByIds(Long[] ids)
    {
        return dischargeMapper.deleteDischargeByIds(ids);
    }

    /**
     * 删除出院管理信息
     * 
     * @param id 出院管理主键
     * @return 结果
     */
    @Override
    public int deleteDischargeById(Long id)
    {
        return dischargeMapper.deleteDischargeById(id);
    }
}
